package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.Resource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class ResourceDao extends BaseDao<Resource> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<Resource> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<Resource> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Resource> list = run.query(sql, new ResultSetHandler<List<Resource>>() {
            @Override
            public List<Resource> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Resource resource = new Resource();

                    resource.setId(rs.getInt("id"));
                    resource.setAmount(rs.getInt("amount"));
                    resource.setCreateTime(rs.getTimestamp("create_time"));
                    resource.setMd5(rs.getString("md5"));
                    resource.setPath(rs.getString("path"));
                    resource.setUpdateTime(rs.getTimestamp("update_time"));

                    list.add(resource);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  Resource 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public Resource selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, amount, create_time, md5, path, update_time from mto_resource where id = ?";
        Resource resource = run.query(sql,new ResultSetHandler<Resource>() {
            @Override
            public Resource handle(ResultSet rs) throws SQLException {
                Resource resource = null;
                if(rs.next()) {
                    resource = new Resource();

                        resource.setId(rs.getInt("id"));
                        resource.setAmount(rs.getInt("amount"));
                        resource.setCreateTime(rs.getTimestamp("create_time"));
                        resource.setMd5(rs.getString("md5"));
                        resource.setPath(rs.getString("path"));
                        resource.setUpdateTime(rs.getTimestamp("update_time"));
                }
                return  resource;
            }
        },params);
        return resource;
    }

    /**
    *
    * @param resource 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(Resource resource) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_resource (  ");
        sql.append("  amount, create_time, md5, path, update_time ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            resource.getAmount(),  
            resource.getCreateTime(),  
            resource.getMd5(),  
            resource.getPath(),  
            resource.getUpdateTime()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  resource
    * @return
    * @throws SQLException
    */
    @Override
    public int update(Resource resource) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_resource           ");
        sql.append(" SET                    ");
            
            sql.append("   amount  = ?,  ");
            sql.append("   create_time  = ?,  ");
            sql.append("   md5  = ?,  ");
            sql.append("   path  = ?,  ");
            sql.append("   update_time  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                resource.getAmount(), 
                resource.getCreateTime(), 
                resource.getMd5(), 
                resource.getPath(), 
                resource.getUpdateTime(), 
            resource.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_resource where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
